﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SupportLibrary;
using System.Xml;
using System.Xml.Linq;
using Common.Logging;

namespace SupportLibrary.XML
{
    public class XMLProvider
    {
        private static ILog log = LogManager.GetLogger(typeof(XMLProvider));

        public static void CreateFileFromTemplate(string path)
        {
            try{
                                //XMLELement e = null;
                XElement e = XElement.Parse(XMLAutomationTemplate.AutomationTemplate);
                e.Save(path);

            }
            catch(Exception e)
            {
                log.Error(Resources.ErrorMessage.XmlCreateFileErr + path);
            }


        }
        //elementName: parent element name of each csv row.
        public static List<XElement> GetElementsFromCsv(string csvfilePath,string elementName) {
            List<XElement> listelements = new List<XElement>();
            try
            {
                
                List<string> listRow = FileIO.ReadFile(csvfilePath);
                List<string> listColumns = FileIO.ReadCsvLine(listRow[0]);
                listRow.RemoveAt(0);
                for(int i = 0;i<listRow.Count; i++){
                    var items = FileIO.ReadCsvLine(listRow[i]);
                   List<XElement> listItem = new List<XElement>();
                    for (int j = 0; j < listColumns.Count; j++)
                    {
                        if (listColumns[j] != string.Empty && j<items.Count)
                        {
                            XElement e = new XElement(listColumns[j], items[j]);
                            listItem.Add(e);
                        }
                    }
                    XElement xelement = new XElement(elementName, listItem);
                    listelements.Add(xelement);
                }
            }
            catch(Exception e){
                log.Error(Resources.ErrorMessage.XmlCreateFileErr + csvfilePath + e.Message);
            }
            return listelements;
        }

        public static void CreateFileFromCsv(string csvfilePath,string xmlFile,string parentElement,string childElement)
        {
            try {

                List<XElement> listElement = GetElementsFromCsv(csvfilePath,childElement);
                XElement parent = new XElement(parentElement, listElement);
                parent.Save(xmlFile);
               
            }
            catch (Exception e) {
                log.Error(Resources.ErrorMessage.XmlCreateFileErr + csvfilePath + e.Message);
            }
        }

        
        public static void AddChilds(string csvfilePath,string xmlFile,string parentElement,string parentkey,string childElement,string referenkey)
        {
            try{
              string listchildElement = childElement + "s";

            List<XElement> listSteps = GetElementsFromCsv(csvfilePath, childElement);
            XElement root = XElement.Load(xmlFile);
            var listcases = root.Elements(parentElement);
            
            foreach (XElement tcase in listcases){
                //tcase.Element[
                XElement name = tcase.Element(parentkey);
              var steps = from step in listSteps
                                       let testcase = step.Element(referenkey)
                                       where testcase.Value.Equals(name.Value)
                                       select step;
              XElement TSteps = new XElement(listchildElement, steps);
                tcase.Add(TSteps);
                //Console.WriteLine((string)name);
            }
            root.Save(xmlFile);

            }
            catch(Exception e)
            {
                log.Error(Resources.ErrorMessage.XmlCreateFileErr + csvfilePath + e.Message);
            }
        }


        
    }
}
